package com.yhk.wuchat.common.common.event.listener;

import com.yhk.wuchat.common.common.event.UserOnlineEvent;
import com.yhk.wuchat.common.common.event.UserRegisterEvent;
import com.yhk.wuchat.common.user.dao.UserDao;
import com.yhk.wuchat.common.user.domain.entity.User;
import com.yhk.wuchat.common.user.domain.enums.IdempotentEnum;
import com.yhk.wuchat.common.user.domain.enums.ItemEnum;
import com.yhk.wuchat.common.user.domain.enums.UserActiveStatusEnum;
import com.yhk.wuchat.common.user.service.IUserBackpackService;
import com.yhk.wuchat.common.user.service.IpService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @version 1.0
 * @Author moresuo
 * @Date 2024/4/16 22:50
 * @注释
 */
@Component
public class UserOnlineListener {
    @Resource
    private IpService ipService;
    @Resource
    private UserDao userDao;
    @TransactionalEventListener(classes = UserOnlineEvent.class,phase = TransactionPhase.AFTER_COMMIT,fallbackExecution = true)
    @Async
    public void saveDB(UserOnlineEvent event){
        User user = event.getUser();
        User update=new User();
        update.setId(user.getId());
        update.setLastOptTime(user.getLastOptTime());
        update.setIpInfo(user.getIpInfo());
        update.setActiveStatus(UserActiveStatusEnum.ONLINE.getStatus());
        userDao.updateById(update);
        //用户ip详情解析
        ipService.refreshIpDetailAsync(user.getId());
    }

}
